package com.liqi.display.controller;


import com.liqi.display.common.Results;
import com.liqi.display.common.SystemCode;
import com.liqi.display.entity.returnType.TopEightTask;
import com.liqi.display.entity.UpProAccept;
import com.liqi.display.service.impl.UpProAcceptServiceImpl;
import com.liqi.display.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *办件受理信息表
 * 统计办理数：
 * 1.当天办理
 * 2.本月办理数
 * 3.本年办理数
 * @author liqi
 * @since 2021-04-12
 */
@RestController
@RequestMapping("/upProAccept/")
public class UpProAcceptController {

    private static final Logger logger = LoggerFactory.getLogger(UpProAcceptController.class);

    @Autowired
    private UpProAcceptServiceImpl upProAcceptService;

    @GetMapping("selectAll")
    public Results selectAll() {
        Results results = new Results();
        try{
            List<UpProAccept> upProAccepts = upProAcceptService.selectAll();
            results.setRecord(upProAccepts);
            results.setCode(SystemCode.SYS_OK.getKey());
            results.setMessage(SystemCode.SYS_OK.getValue());
            logger.info("查询结果:{}",results);
            return results;
        }catch (Exception e){
            results.setCode(SystemCode.SYS_FAIL.getKey());
            results.setMessage(SystemCode.SYS_FAIL.getValue());
            logger.info("查询结果:{}",results);
            return results;
        }
    }

    @RequestMapping("total")
    public Results total(){
        Results results=new Results();

        //日
        Date day = DateUtil.getDay();
        int totalDay = upProAcceptService.total(day);

        //月
        Date month = DateUtil.getMonth();
        int totalMonth = upProAcceptService.total(month);

        //年
        Date year = DateUtil.getYear();
        int totalYear = upProAcceptService.total(year);
        try {
            Map<String,Integer> map=new ConcurrentHashMap<String,Integer>();
            map.put("day",totalDay);
            map.put("month",totalMonth);
            map.put("year",totalYear);
            logger.info("统计结果",map);
            results.setData(map);
            results.setCode(SystemCode.SYS_OK.getKey());
            results.setMessage(SystemCode.SYS_OK.getValue());
            return results;
        }catch (Exception e){
            results.setCode(SystemCode.SYS_FAIL.getKey());
            results.setMessage(SystemCode.SYS_FAIL.getValue());
            logger.info("受理信息数异常",e.getMessage());
            return results;
        }
    }

    /**
     * 汇总当月前8热门事项
     * @return
     */
    @GetMapping("topEightTask")
    public Results topEightTask(){
        Results results=new Results();
        try{
            List<TopEightTask> topEightTasks = upProAcceptService.topEightTask(DateUtil.getMonth());
            Map<String,Object>map =new ConcurrentHashMap<>();
            map.put("topEightTask",topEightTasks);
            results.setData(map);
            results.setCode(SystemCode.SYS_OK.getKey());
            results.setMessage(SystemCode.SYS_OK.getValue());
            return results;
        }catch (Exception e){
            results.setCode(SystemCode.SYS_FAIL.getKey());
            results.setMessage(SystemCode.SYS_FAIL.getValue());
            logger.info("异常信息",e.getMessage());
            return results;
        }
    }


    /**
     * 获取当月平均受理时长,通过申办时间、受理时间
     * @return
     */
    @GetMapping("getList")
    public Results getList(){
        Results results=new Results();
        try {
            //获取当前月的制定时间
            Date month = DateUtil.getBeforeMonthFirst();
            long minutes = upProAcceptService.getList(month);
            results.setData(minutes);
            results.setCode(SystemCode.SYS_OK.getKey());
            results.setMessage(SystemCode.SYS_OK.getValue());
            return results;
        }catch (Exception e){
            results.setData(null);
            results.setCode(SystemCode.SYS_FAIL.getKey());
            results.setMessage(SystemCode.SYS_FAIL.getValue());
            return results;
        }
    }

   @RequestMapping("lateThirty")
    public Results lateThirty(){
       Results r=new Results();
        try {
            Map<String, Integer> map = upProAcceptService.lateThirty();
            r.setData(map);
            r.setCode(SystemCode.SYS_OK.getKey());
            r.setMessage(SystemCode.SYS_OK.getValue());
            return r;
        }catch (Exception e){
            r.setCode(SystemCode.SYS_OK.getKey());
            r.setMessage(SystemCode.SYS_OK.getValue());
            return r;
        }
   }

   @RequestMapping("totalAcceptType")
    public Results totalAcceptType(){
        Results r = new Results();
       try {
           Map<String, Integer> map = upProAcceptService.totalAcceptType();
           r.setData(map);
           r.setCode(SystemCode.SYS_OK.getKey());
           r.setMessage(SystemCode.SYS_OK.getValue());
           return r;
       }catch (Exception e){
           r.setCode(SystemCode.SYS_OK.getKey());
           r.setMessage(SystemCode.SYS_OK.getValue());
           return r;
       }
   }

   @RequestMapping("specilAcceptTotal")
    public Results specilAcceptTotal(){
        Results r = new Results();
       Map<String, Integer> map = upProAcceptService.specilAcceptTotal();
       r.setData(map);
       r.setCode(SystemCode.SYS_OK.getKey());
       r.setMessage(SystemCode.SYS_OK.getValue());
       return r;
   }
}
